﻿using Architecture.Repository.Interface;
using Architeture.Domain;
using Architeture.Domain.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Architecture.Domain.Service
{
    public class MenuSerivice : IMenuSerivice
    {
        protected readonly MenuIrepsitory menuRep;

        public MenuSerivice(MenuIrepsitory menuRep)
        {
            this.menuRep = menuRep;
        }

        public async Task<int> DeleteMenu(int id)
        {
            var digui = (await menuRep.GetAllAsync()).Any(x => x.Pid == id);
            //是否有子集
            if (digui)
            {
                delId(id);
                //循环删除子集
                var list = (await menuRep.GetAllAsync()).Where(x => x.Pid == id).ToList();
                foreach (var item in list)
                {
                    await DeleteMenu(item.MenuInfoid);
                }
            }
            else
            {
                menuRep.MenuInfoDel(id);
            }
            return 1;
        }
        public void delId(int id)
        {
            menuRep.MenuInfoDel(id);
        }



        public async Task<MenuPage> GetMenuAll(int pageindex, int pagesize, string? name)
        {
            var size = (pageindex - 1) * pagesize;
            var list = await menuRep.GetAllAsync();
            var newlist = (list.Select(x => new menuDTo
            {
                MenuInfoid = x.MenuInfoid,
                MenuInfoName = x.MenuInfoName,
                Pid = x.Pid,
                Url = x.Url,
                CreateDate = x.CreateDate,
                CreateBy = x.CreateBy,
                UpdateBy = x.UpdateBy,
                UpdateDate = x.UpdateDate,
                value = list.Where(a => a.MenuInfoid == x.Pid).Select(q => new newMenu
                {
                    MenuInfoName = q.MenuInfoName
                }).ToList()
            })).ToList();
            if (name != null)
            {
                newlist = newlist.Where(x => x.MenuInfoName.Contains(name)).ToList();
            }
            var page = new MenuPage();
            page.pagecount = newlist.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / pagesize);
            newlist = newlist.OrderBy(x => x.CreateDate).Skip(size).Take(pagesize).ToList();

            page.list = newlist;
            return page;
        }

        public async Task<MenuInfo> GetMenuByid(int id)
        {
            return await menuRep.GetMenuByid(id);
        }

        public async Task<int> MennuAdd(MenuInfo a)
        {
            var list = (await menuRep.GetAllAsync()).FirstOrDefault(x => x.MenuInfoid == a.MenuInfoid);
            if (list != null)
            {
                return -1;
            }
            else
            {
                a.CreateBy = "张三";
                a.CreateDate = DateTime.Now.ToString();
                return await menuRep.AddAsync(a);
            }

        }

        public async Task<int> MenuDelete(MenuInfo a)
        {
            a.UpdateBy = "李四";
            a.UpdateDate = DateTime.Now.ToString();
            return await menuRep.Update(a);
        }



        public async Task<int> MenuInfoDelis(int id)
        {
            var list = (await menuRep.GetAllAsync()).Count(x => x.Pid == id);
            if (list > 0)
            {
                return 1;
            }
            else
            {
                return -2;
            }
        }






        /// <summary>
        /// 测试多少个父级
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>      
        public async Task<int> GetInfoList(int id)
        {
            var menulist = await menuRep.GetAllAsync();
            var newid = await menuRep.GetMenuByid(id);
            var allfather = GetMenu(menulist.ToList(), newid.Pid);
            return 1;
        }
        public static List<MenuInfo> GetMenu(List<MenuInfo> info, int id)
        {
            var father = info.Where(x => x.MenuInfoid == id).ToList();
            List<MenuInfo> newfather = new List<MenuInfo>();
            foreach (var item in father)
            {
                newfather.AddRange(GetMenu(info, item.Pid));
            }
            father.AddRange(newfather);
            return father;
        }

        public async Task<List<mentumns>> GetMenuTree(int mid = 0)
        {
            var MenuTree = (await menuRep.GetAllAsync()).Where(w => w.Pid == mid);
            var newtree = MenuTree.Select(x => new mentumns
            {
                value = x.MenuInfoid,
                label = x.MenuInfoName,
                children = GetMenuTree(x.MenuInfoid).Result
            }).ToList();
            return newtree;
        }
        public async Task<List<MenuRoleTreeDto>> GetRoleMenuTree(int mid = 0)
        {
            var MenuTree = (await menuRep.GetAllAsync()).Where(w => w.Pid == mid);
            var newtree = MenuTree.Select(x => new MenuRoleTreeDto
            {
                value = x.MenuInfoid,
                label = x.MenuInfoName,
                Address = x.Url,
                children = GetRoleMenuTree(x.MenuInfoid).Result
            });
            return newtree.ToList();
        }

        public async Task<List<MenuInfo>> GetSelectMenu()
        {
            return (await menuRep.GetAllAsync()).ToList();
        }
    }
}
